package com.zhan.ielts.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.zhan.frame.core.dao.impl.BaseDaoImpl;
import com.zhan.frame.core.exception.DaoException;
import com.zhan.frame.core.utils.BeanUtils;
import com.zhan.ielts.dao.SentenceWordDao;
import com.zhan.ielts.domain.SentenceWord;
import com.zhan.ielts.domain.WordCategory;
import com.zhan.ielts.domain.WordScene;
import com.zhan.ielts.domain.query.SentenceWordQuery;
import com.zhan.ielts.domain.vo.SentenceWordVo;

@Repository
public class SentenceWordDaoImpl extends BaseDaoImpl<SentenceWord> implements
		SentenceWordDao {

	@Override
	public List<String> selectType(SentenceWord query) {
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return sqlSessionTemplate.selectList(getSqlName("selectType"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询type出错！语句：%s",
					getSqlName("selectType")), e);
		}

	}

	@Override
	public List<String> selectPassage(SentenceWord query) {
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return sqlSessionTemplate.selectList(getSqlName("selectPassage"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询type出错！语句：%s",
					getSqlName("selectPassage")), e);
		}
	}

	@Override
	public void saveSubject(WordScene wordSubject) {
		try {
			sqlSessionTemplate.insert(getSqlName("saveSubject"), wordSubject);
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s",
					getSqlName("saveSubject")), e);
		}
	}

	@Override
	public void deleteSubject(WordScene wordSubject) {
		try {
			sqlSessionTemplate.delete(getSqlName("deleteSubject"), wordSubject);
		} catch (Exception e) {
			throw new DaoException(String.format("删除对象出错！语句：%s",
					getSqlName("deleteSubject")), e);
		}
	}

	@Override
	public List<WordScene> selectSubject(WordScene wordSubject) {
		try {
			Map<String, Object> params = BeanUtils.toMap(wordSubject);
			return sqlSessionTemplate.selectList(getSqlName("selectSubject"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询type出错！语句：%s",
					getSqlName("selectSubject")), e);
		}
	}

	@Override
	public void saveCategory(WordCategory wordCategory) {
		try {
			sqlSessionTemplate.insert(getSqlName("saveCategory"), wordCategory);
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s",
					getSqlName("saveCategory")), e);
		}
	}

	@Override
	public void deleteCategory(WordCategory wordCategory) {
		try {
			sqlSessionTemplate.delete(getSqlName("deleteCategory"),
					wordCategory);
		} catch (Exception e) {
			throw new DaoException(String.format("删除对象出错！语句：%s",
					getSqlName("deleteCategory")), e);
		}
	}

	@Override
	public List<WordCategory> selectCategory(WordCategory wordCategory) {
		try {
			Map<String, Object> params = BeanUtils.toMap(wordCategory);
			return sqlSessionTemplate.selectList(getSqlName("selectCategory"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询type出错！语句：%s",
					getSqlName("selectCategory")), e);
		}
	}

	@Override
	public PageImpl<SentenceWordVo> selectPageListCustom(
			SentenceWordQuery query, Pageable pageable) {
		try {
			List<SentenceWordVo> contentList = sqlSessionTemplate.selectList(
					getSqlName("selectCustom"), getParams(query, pageable));
			return new PageImpl<SentenceWordVo>(contentList, pageable,
					this.selectCountCustom(query));
		} catch (Exception e) {
			throw new DaoException(String.format("根据分页对象查询列表出错！语句:%s",
					getSqlName("selectCustom")), e);
		}
	}

	public Long selectCountCustom(SentenceWordQuery query) {
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return sqlSessionTemplate.selectOne(
					getSqlName("selectCountCustom"), params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象总数出错！语句：%s",
					getSqlName("selectCountCustom")), e);
		}
	}

	@Override
	public Integer selectCountSeqNo(SentenceWordQuery query) {
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return sqlSessionTemplate.selectOne(getSqlName("selectCountSeqNo"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象总数出错！语句：%s",
					getSqlName("selectCountSeqNo")), e);
		}
	}

	@Override
	public void updateSeqNo(SentenceWord entity) {
		try {
			sqlSessionTemplate.update(getSqlName("updateSeqNo"), entity);
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s",
					getSqlName("updateSeqNo")), e);
		}
	}

	@Override
	public Long selectWordCount(SentenceWordQuery query) {
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return sqlSessionTemplate.selectOne(getSqlName("selectWordCount"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象总数出错！语句：%s",
					getSqlName("selectWordCount")), e);
		}
	}
}
